<?php
/*
Plugin Name: Slider Banner
Plugin URI: 
Version: v1.0
Author: <a href="http://huynhuan.wordpress.com">HuanHH</a>
Description: Power by bizonline.com.
*/
?>
<?php 
class SliderBanner extends WP_Widget {

	var $table_bannerize = 'vn_bannerize_a';
	function SliderBanner() {
		$widget_ops = array('classname' => 'slider_banner', 'description' => 'Slider Banner' );
		$control_ops = array ('width' => '200', 'height' => '400');
		$this->WP_Widget('slider_banner', 'Slider Banner', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance) {
		extract($args);
		extract($instance);
		global $wpdb;
		
		$q = "SELECT * FROM `".$this->table_bannerize."` WHERE `trash` = '0' ";

		if( $group != "") $q .= " AND `group` = '" . $group. "'";

		/**
		 * Add random option
		 *
		 * @since 2.0.2
		 */
		$q .= ($random == "" ) ? " ORDER BY `sorter` ASC" : "ORDER BY RAND()";

		/**
		 * Limit rows number
		 *
		 * @since 2.0.0
		 */
		if( $limit != "") $q .= " LIMIT 0," . $limit ;

		$rows = $wpdb->get_results( $q );
		//print "<pre>";
		//print_r($rows);
		//print "</pre>";
		?>
		<div id="primary" class="hfeed" style="margin-bottom: 5px">
			<div id="sliderbanner" style="width: 100%; text-align: center">
				<?php if (count($rows) > 0) : ?>
					<?php foreach ($rows as $k => $v) : ?>
						<a title="<?php print $v->description?>" href="<?php print $v->url?>"><img width="<?php print !empty($imgw) ? $imgw : $v->width;?>" height="<?php print !empty($imgh) ? $imgh : $v->height;?>" alt="" src="<?php print $v->filename; ?>" /></a>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
			<script type="text/javascript" src="<?php print get_bloginfo('template_url'); ?>/js/jquery.cycle.all.min.js" ></script>
			<script type="text/javascript">
				jQuery(document).ready(function() {
					jQuery('#sliderbanner').cycle({
						fx: 'scrollLeft'
					});
				});
			</script>
		</div>
		<?php
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;

		$group = $new_instance['group'];
		$limit = $new_instance['limit'];
		$random = $new_instance['random'] ? 1 : 0;
		$imgw = $new_instance['imgw'];
		$imgh = $new_instance['imgh'];
		
		$instance = compact('group', 'limit', 'random', 'imgw', 'imgh');
		return $instance;
	}

	function form($instance) {
		$defaults=array(
			'group' => '',
			'random' => 0,
			'limit' => 3,
			'imgw' => 100,
			'imgh' => 100
		);
		$options = wp_parse_args($instance, $defaults);
		extract($options);
		?>

		<div>
			<div>Group:</div>
			<div><?php echo $this->get_group( $group ) ?></div>
			<div>Random:</div>
			<div><input <?php echo ($random == '1') ? 'checked="chekced"' : '' ?> value="1" type="checkbox" name="<?php echo $this->get_field_name('random'); ?>" id="<?php echo $this->get_field_id('random'); ?>" /></div>
			<div>Limit:</div>
			<div><input  id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo attribute_escape($limit); ?>" /></div>
			<div>Image width:</div>
			<div><input  id="<?php echo $this->get_field_id('imgw'); ?>" name="<?php echo $this->get_field_name('imgw'); ?>" type="text" value="<?php echo attribute_escape($imgw); ?>" /></div>
			<div>Image height:</div>
			<div><input  id="<?php echo $this->get_field_id('imgh'); ?>" name="<?php echo $this->get_field_name('imgh'); ?>" type="text" value="<?php echo attribute_escape($imgh); ?>" /></div>
		</div>

		<?php
	}

	function get_group($group = '' ) {
		global $wpdb;
		$o = '<select rel="'.$group.'" id="' . $this->get_field_id('group') . '" name="' . $this->get_field_name('group')  . '">' .
		    '<option value=""></option>';
		$q = "SELECT `group` FROM `" . $this->table_bannerize . "` GROUP BY `group` ORDER BY `group` ";
		$rows = $wpdb->get_results( $q );
		foreach( $rows as $row ) {
		    $sel = ($group == $row->group) ? 'selected="selected"' : "" ;
		    $o .= '<option ' . $sel . ' value="' . $row->group . '">' . $row->group . '</option>';
		}
		$o .= '</select>';
		return $o;
	    }
}

if (class_exists("SliderBanner")) {
	$slider_banner = new SliderBanner();
}

function register_slider_banner() {
	register_widget('SliderBanner');
}
add_action('widgets_init', 'register_slider_banner');

?>
